package com.nice.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.nice.Commons;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
public class ChatController {


   public  Map<String, List<String>> chatmap = new HashMap<String, List<String>>();

    /**
     * 跳转价格列表页面
     * @return
     */
    @GetMapping("/chatHtml")

    public ModelAndView chathtml() {
        return new ModelAndView("chat.html");
    }


    @GetMapping("/sendMsg")
    @ResponseBody
    public String sendMsg(HttpSession session,String text) {
        Date dNow = new Date( );
        SimpleDateFormat ft = new SimpleDateFormat ("MMdd/hh:mm");
        System.out.println("当前时间为: " + ft.format(dNow)+"收到消息");

        JSONObject json = new JSONObject();
        List<String> newList = new ArrayList<String>();
        //取登录名
        String loginName = String.valueOf(session.getAttribute(Commons.LOGIN_NAME));

        if (null == loginName || "".equals(loginName)) {
            loginName = "nologin";
            json.put("text", loginName);
            return json.toJSONString();
        }

        List<String> chatmapList = chatmap.get(loginName);
        //有聊天记录未被读阅，则继续追加内容
        if (chatmapList != null) {
            chatmapList.add(loginName+"-"+ft.format(dNow)+"说:"+text);
            chatmap.put(loginName, chatmapList);
            json.put("text", "发送成功");
        }else{
            //无留言，生成一份到内存
            newList.add(loginName+"-"+ft.format(dNow)+"说:"+text);
            chatmap.put(loginName, newList);
            json.put("text", "发送成功");
        }
//        json.put("text", "asdfasdaf");
        return json.toJSONString();
    }

    /**
     * 登陆接口-设置会话
     * @param session
     * @return
     */
    @PostMapping("/queryMsg")
    @ResponseBody
    public String queryMsg(HttpSession session){
        JSONObject json = new JSONObject();
        //取登录名
        String loginName = String.valueOf(session.getAttribute(Commons.LOGIN_NAME));

        if (null == loginName || "".equals(loginName)) {
            loginName = "nologin";
            json.put("text", loginName);
            return json.toJSONString();
        }

        List<String> newList = new ArrayList<String>();

        Set<String> keyset = chatmap.keySet();
        for (String key : chatmap.keySet()) {
            if (key != null && key.equals(loginName)) {
                continue;
            }
            newList.addAll(chatmap.get(key));
        }

        if (newList != null && newList.size() > 0) {
            json.put("text", newList);
            System.out.println(json.toJSONString());
        }else{
            json.put("text", "nodata");
            System.out.println(json.toJSONString());
        }


        for (String key : keyset) {
            if (key != null && key.equals(loginName)) {
                continue;
            }
            chatmap.remove(key);
        }

        return json.toJSONString();
    }


    /**
     * post 方法获取登录用户的邮箱
     *
     * @return
     */
    @GetMapping("/queryLoginNameWithSeriNo")
    @ResponseBody
    public String queryLoginNameWithSeriNo(HttpSession session) {
        String loginName = String.valueOf(session.getAttribute(Commons.LOGIN_NAME));
        if (null == loginName || "".equals(loginName)) {
            loginName = "nologin";
        }
        return loginName;
    }
}
